package com.zy.fcs.rest;

import java.io.IOException;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zy.fcs.bean.GetMedia;
import com.zy.fcs.bean.param.*;
import com.zy.fcs.controller.BaseController;
import com.zy.fcs.service.MediaManagementService;
import com.zy.fcs.service.ZtreeService;
import com.zy.fcs.service.oss.OssService;

import io.swagger.annotations.ApiOperation;

/**
 * 影像系统对外接口
 *
 * @author LYN
 * @date 2017/3/19
 */
@RestController
public class FcsRestController extends BaseController {

    @Autowired
    private MediaManagementService mediaManagementService;

    @Autowired
    private ZtreeService ztreeService;

    @Autowired
    private OssService ossService;

    private static final Logger logger = LoggerFactory.getLogger(FcsRestController.class);

    /**
     * 根据机构号获得影像目录结构
     *
     * @param param 机构号
     * @return 树状形式的目录
     */
    @ApiOperation(value = "根据机构号获得影像目录结构")
    @PostMapping(value = "/selectMenu")
    public JSONObject selectMenu(@RequestBody SelectMenuParam param) {
        JSONObject result = successJson();
        try {
            JSONArray tree = ztreeService.getTreeJson(param.getInstNo(), param.getOrderNo());
            result.put("docTree", tree);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return failedJson();
        }

        return result;
    }

    /**
     * 获取上传文件Policy
     *
     * @param param 文件上传Policy参数
     * @return policy
     */
    @ApiOperation(value = "获取上传文件Policy")
    @PostMapping(value = "/restGetPolicy")
    public JSONObject getUploadPolicy(@RequestBody GetUploadPolicyParam param) throws IOException {
        JSONObject result = successJson();
        JSONObject policy = ossService.getUploadPolicy(param.getUploadDir());
        if (policy == null) {
            return failedJson();
        }else {
            result.put("policy", policy);
        }

        return result;
    }

    /**
     * 文件上传后的处理
     *
     * @param param 上传后处理参数
     * @return 成功消息
     */
    @ApiOperation(value = "文件上传后的处理")
    @PostMapping(value = "/afterUpload")
    public JSONObject afterUpload(@RequestBody AfterUploadParam param) {
        try {
            mediaManagementService.afterUpload(param);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return failedJson();
        }
        return successJson();
    }

    /**
     * 获得订单下所有影像文件路径
     * 
     * @param param 机构号，订单号
     * @return 订单下所有影像文件路径
     */
    @ApiOperation(value = "获得订单下所有影像文件路径")
    @PostMapping(value = "/getMediaUrls")
    public JSONObject getMediaUrls(@RequestBody GetMediaUrlsParam param) {
        JSONObject result = successJson();
        try {
            List<GetMedia> mediaList = mediaManagementService.getMediaUrls(param);
            result.put("medias", mediaList);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return failedJson();
        }

        return result;
    }

    /**
     * 删除文件（只删除数据，不删除oss文件）
     *
     * @param param 主键
     */
    @ApiOperation(value = "删除影像文件")
    @PostMapping(value = "/restDeleteMedia")
    public JSONObject deleteMedia(@RequestBody DeleteMedia param) {
        try {
            mediaManagementService.deleteMedia(Long.valueOf(param.getId()));
        } catch (Exception e) {
            logger.error(e.getMessage());
            return failedJson();
        }

        return successJson();
    }
    @ApiOperation(value = "获取ststoken")
    @PostMapping(value = "/getOssStsToken")
    public JSONObject getOssStsToken(@RequestBody GetUploadPolicyParam param) {
        JSONObject result = successJson();
        JSONObject policy = ossService.getStsToken(param.getUploadDir());
        if (policy == null) {
            return failedJson();
        }else {
            result.put("policy", policy);
        }
        return result;
    }
}
